package com.qpkj.web.controller.oms;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.qpkj.common.constant.UserConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qpkj.common.annotation.Log;
import com.qpkj.common.core.controller.BaseController;
import com.qpkj.common.core.domain.AjaxResult;
import com.qpkj.common.enums.BusinessType;
import com.qpkj.oms.domain.OmsContract;
import com.qpkj.oms.service.IOmsContractService;
import com.qpkj.common.utils.poi.ExcelUtil;
import com.qpkj.common.core.page.TableDataInfo;

/**
 * 合同信息Controller
 * 
 * @author 光永
 * @date 2023-02-18
 */
@RestController
@RequestMapping("/oms/contract")
public class OmsContractController extends BaseController
{
    @Autowired
    private IOmsContractService omsContractService;

    /**
     * 查询合同信息列表
     */
    @PreAuthorize("@ss.hasPermi('oms:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(OmsContract omsContract)
    {
        startPage();
        List<OmsContract> list = omsContractService.selectOmsContractList(omsContract);
        return getDataTable(list);
    }

    /**
     * 导出合同信息列表
     */
    @PreAuthorize("@ss.hasPermi('oms:contract:export')")
    @Log(title = "合同信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OmsContract omsContract)
    {
        List<OmsContract> list = omsContractService.selectOmsContractList(omsContract);
        ExcelUtil<OmsContract> util = new ExcelUtil<OmsContract>(OmsContract.class);
        util.exportExcel(response, list, "合同信息数据");
    }

    /**
     * 获取合同信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('oms:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(omsContractService.selectOmsContractById(id));
    }

    /**
     * 新增合同信息
     */
    @PreAuthorize("@ss.hasPermi('oms:contract:add')")
    @Log(title = "合同信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody OmsContract omsContract)
    {
        if (UserConstants.NOT_UNIQUE.equals(omsContractService.checkcontractNoUnique(omsContract))) {
            return error("新增合同'" + omsContract.getContractNo() + "'失败，该合同编号已存在");
        }
        return toAjax(omsContractService.insertOmsContract(omsContract));
    }

    /**
     * 修改合同信息
     */
    @PreAuthorize("@ss.hasPermi('oms:contract:edit')")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OmsContract omsContract)
    {
        return toAjax(omsContractService.updateOmsContract(omsContract));
    }

    /**
     * 删除合同信息
     */
    @PreAuthorize("@ss.hasPermi('oms:contract:remove')")
    @Log(title = "合同信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(omsContractService.deleteOmsContractByIds(ids));
    }
}
